package lego.jbv;

import java.awt.TextField;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.JLabel;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;
import lejos.util.Delay;

public class CommunicateThread extends Thread {
	private int left = 0, right = 0;
	private boolean sendValueNow = false;

	private String name = "Desire";
	private String address = "00165308d166";

	private NXTComm nxtComm;
	private NXTInfo nxtInfo;
	private OutputStream os;
	private DataOutputStream dos;
	private ArrayBlockingQueue<Integer[]> queue;
	
	public CommunicateThread(ArrayBlockingQueue<Integer[]> _queue)
	{
		queue = _queue;
	}

	public void run() {
		Connect();
		
		while (true) {
			try {
				Integer[] values = queue.take();
				dos.writeInt(values[0]);

				dos.writeInt(values[1]);
				dos.flush();
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		}

	}

	private void Connect() {
		nxtInfo = new NXTInfo();
		nxtInfo.name = name;
		nxtInfo.deviceAddress = address;
		try {

			nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
			nxtComm.open(nxtInfo);
			os = nxtComm.getOutputStream();
			dos = new DataOutputStream(os);
		} catch (Exception ex) {
		}
	}

}
